<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <script>
        /********** 其他类型转字符串 *************/
        // 1. toString()
        var num1 = 10;
        var str1 = num1.toString();
        console.log(str1, typeof str1);

        // 2. String()
        var str2 = String(num1)
        console.log(str2, typeof str2);

        // 3. '+' 拼接字符串(隐式转换)
        var str3 = num1 + ''
        console.log(str3, typeof str3);

        /********** 其他类型转数字 *************/
        var age = '100'; //prompt('输入年龄');

        // 1. parseInt() 字符转数字 只能得到整数
        var number1 = parseInt(age);
        console.log(number1, typeof number1);
        console.log(parseInt('120px'));
        console.log(parseInt('re121px'));

        // 2. parseFloat() 字符转数字 可以得到浮点数
        var number2 = parseFloat(age);
        console.log(number2, typeof number2);
        console.log(parseFloat('121px'));
        console.log(parseFloat('re121px'));

        // 3. Number() 强制转换
        var str = '123';
        console.log(Number(str));
        console.log(Number('123'));

        // 4. 利用运算符 '-、+、*、/' 隐式转换
        console.log('12' - 0);
        console.log('199' - '120');

        // 案例 1
        // var currentAge = 2021 - parseFloat(prompt('年龄'));
        var currentAge = 2021 - prompt('年龄');
        alert('你今年' + currentAge + '岁');

        // 案例 2
        var one = prompt('请输入数字1');
        var tow = prompt('请输入数字2');
        var num99 = parseFloat(one) + parseFloat(tow);
        alert('总和' + num99);

        /********** 其他类型转bool型 *************/
        console.log(Boolean(''));
        console.log(Boolean(0));
        console.log(Boolean(NaN));
        console.log(Boolean(null));
        console.log(Boolean(undefined));
        console.log('----------------');
        console.log(Boolean('haha'));
        console.log(Boolean(12));

        // 作业
        var inputName = prompt('姓名');
        var inputAge = parseFloat(prompt('出生年份'));
        var age100 = 2021 - inputAge;
        var sex = prompt('性别');
        alert('姓名' + inputName + '\n' + '出生年份' + inputAge + '\n' + '年龄' + age100 + '\n' + '性别' + sex);
    </script>

</head>

<body>

</body>

</html>